package com.itheima.controller;


import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author CHEN ZHI
 * @since 2020-08-05
 */
@RestController
@RequestMapping("/sysDictType")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST}, origins = "*")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @PostMapping("/add")
    public Result add(@RequestBody SysDictType sysDictType) {
        try {
            sysDictTypeService.add(sysDictType);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "添加数据失败");
        }
        return new Result("success", "添加数据成功");
    }

    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody SysDictType sysDictType) {
        try {
            sysDictTypeService.deleteBatch(sysDictType);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "删除数据失败");
        }
        return new Result("success", "删除数据成功");
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody SysDictType sysDictType) {

        try {
            sysDictTypeService.edit(sysDictType);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "修改数据失败");
        }
        return new Result("success", "修改数据成功");
    }


    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> searchMap) {

        Page<SysDictType> pageList = sysDictTypeService.getList(searchMap);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setTotal((int) pageList.getTotal());
        result.setRecords(pageList.getResult());
        return new Result("success", result);

    }


}

